package com.casit.base.entity;


import com.casit.base.util.UserUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@MappedSuperclass
public class BaseEntity<T> implements Serializable {

    public static char FLAG_NORMAL_ = '5';
    public static char FLAG_DELETE_ = '4';

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "x_insert", updatable = false)
    @JsonIgnore
    private Date xInsertDate;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "x_update")
    @JsonIgnore
    private Date xUpdateDate;
    @Temporal(TemporalType.TIMESTAMP)

    @Column(name = "x_delete", updatable = false)
    private Date xDeleteDate;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "x_send")
    private Date xSend;
    @Column(name = "x_version")
    private int xVersion;
    @Column(name = "x_createby", updatable = false)
    private String xCreateBy;
    @Column(name = "x_updateby")
    private String xUpdateBy;
    @Column(name = "x_deleteby")
    private String xDeleteBy;
    @Column(name = "x_flag")
    private char xFlag;

    public BaseEntity() {
        xInsertDate = new Date();
        xFlag = FLAG_NORMAL_;
        xVersion = 0;
    }

    @PrePersist
    public void prePersist() {
        this.xCreateBy = UserUtil.getCurrentUserId();
        this.xUpdateBy = UserUtil.getCurrentUserId();
        this.xInsertDate = new Date();
        this.xUpdateDate = this.xInsertDate;
    }

    @PreUpdate
    public void preUpdate() {
        this.xUpdateDate = new Date();
        this.xUpdateBy= UserUtil.getCurrentUserId();
    }

    @PreRemove
    public void preDelete() {
        this.xDeleteDate = new Date();
        this.xDeleteBy = UserUtil.getCurrentUserId();;
    }

    public Date getxInsertDate() {
        return xInsertDate;
    }

    public void setxInsertDate(Date xInsertDate) {
        this.xInsertDate = xInsertDate;
    }

    public Date getxUpdateDate() {
        return xUpdateDate;
    }

    public void setxUpdateDate(Date xUpdateDate) {
        this.xUpdateDate = xUpdateDate;
    }

    public Date getxDeleteDate() {
        return xDeleteDate;
    }

    public void setxDeleteDate(Date xDeleteDate) {
        this.xDeleteDate = xDeleteDate;
    }

    public Date getxSend() {
        return xSend;
    }

    public void setxSend(Date xSend) {
        this.xSend = xSend;
    }

    public int getxVersion() {
        return xVersion;
    }

    public void setxVersion(int xVersion) {
        this.xVersion = xVersion;
    }

    public String getxCreateBy() {
        return xCreateBy;
    }

    public void setxCreateBy(String xCreateBy) {
        this.xCreateBy = xCreateBy;
    }

    public String getxUpdateBy() {
        return xUpdateBy;
    }

    public void setxUpdateBy(String xUpdateBy) {
        this.xUpdateBy = xUpdateBy;
    }

    public String getxDeleteBy() {
        return xDeleteBy;
    }

    public void setxDeleteBy(String xDeleteBy) {
        this.xDeleteBy = xDeleteBy;
    }

    public char getxFlag() {
        return xFlag;
    }

    public void setxFlag(char xFlag) {
        this.xFlag = xFlag;
    }
}
